﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Samples.BcdSampleLib;
using System.IO;
using System.Diagnostics;

namespace BCDVHD
{
    public partial class frmVHD : Form
    {
        public frmVHD()
        {
            InitializeComponent();
        }

        private void btnVHD_Click(object sender, EventArgs e)
        {
            if (ofdVHD.ShowDialog(this) == DialogResult.OK)
            {
                txtVHD.Text = ofdVHD.FileName;
            }
        }

        private void frmVHD_Load(object sender, EventArgs e)
        {
            DisplayPrimaryBoot();
        }

        private void DisplayPrimaryBoot()
        {
            cmbBoot.DataSource = BCDHelper.EnumerateEntries();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (cmbBoot.Items.Count == 0)
                return;
            var kvSel = (KeyValuePair<string,string>)cmbBoot.SelectedItem;
            string osEntryId = kvSel.Key;
            bool success =BcdStore_API.DeleteOsEntry(osEntryId);

            if (success)
            {
                MessageBox.Show("successful deletion,now cleanup");
                // clean out any guid references in the display list, to avoid a COM error for invalid guid list item in display list pointing to
                // a guid which no longer exists in the store.

                List<string> guidDisplayList = BcdStore_API.GetBootManagerGUIDEntriesDisplayListAsList();
                bool badOneInDisplayList = false;
                List<string> moddedList = new List<string>();
                foreach (string guid in guidDisplayList)
                {
                    if (guid == osEntryId)
                    {
                        badOneInDisplayList = true;
                    }
                    else
                        moddedList.Add(guid);
                }
                if (badOneInDisplayList)
                {
                    string errorDetails;

                    success = BcdStore_API.SetOSDisplayListGuids(moddedList.ToArray(), "", out errorDetails);
                    if (!success)
                    {
                        MessageBox.Show("deletion went thru okay, but trouble removing some of the guid references in the display list");
                        return;
                    }

                }
                

            }
            else
            {
                MessageBox.Show("failure to delete");
            }

            DisplayPrimaryBoot();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (cmbBoot.Items.Count == 0)
                return;
            string vhd = txtVHD.Text;
            if (!File.Exists(vhd))
            {
                MessageBox.Show(this, string.Format("The vhd file {0} does not exists", vhd));
                return;
            }
            
            var kvSel = (KeyValuePair<string, string>)cmbBoot.SelectedItem;

            BCDHelper.CreateCopy(kvSel.Key, vhd,Path.GetFileNameWithoutExtension(vhd));
            DisplayPrimaryBoot();
        }
        private void btnTools_Click(object sender, EventArgs e)
        {
            ctsLinks.Show(btnTools, 0, 0);
        }

        private void ctsLinks_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            Process.Start(e.ClickedItem.Tag.ToString());
        }

    }
}
